package com.vn.ducktohome.screen.play;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.vn.ducktohome.constant.Constant;
import com.vn.ducktohome.enums.StatusEnum;
import com.vn.ducktohome.gamelogic.GameControl;
import com.vn.ducktohome.helper.SoundManager;
import com.vn.ducktohome.screen.PlayScreen;

public class DynamicDuck extends Image {
	private DuckGroup parent;
	private TextureRegionDrawable drawable;
	
	private int r = 210;
	private float angle = Constant.FIRST_ANGLE;
	private int x, y;
	//Neu xyCollision cham ca sau, vit bi ca sau an.
	private float xCollision = 0, yCollision = 0;
	private StatusEnum run = StatusEnum.RUNNING;
	private boolean bOneCollision = false;
	
	//test
	public Image point;

	public DynamicDuck(DuckGroup parent) {
		super();

		bOneCollision = false;
		
		this.parent = parent;
		drawable = new TextureRegionDrawable();
		drawable.setRegion(GameControl.getAtlas().findRegion("play/duck", 1));
		
		setDrawable(drawable);
		setWidth(drawable.getMinWidth());
		setHeight(drawable.getMinHeight());
//		setVisible(false);
		
		point = new Image(GameControl.getAtlas().findRegion("play/point"));
	}
	
//	public void init() {
//		drawable.setRegion(GameControl.getAtlas().findRegion("play/duck", 1));
		
//		angle = Constant.FIRST_ANGLE;
//		x = (int) (r * Math.sin(angle * 2 * Math.PI / 360.0)) + Constant.X_CENTER; 
//		y = (int) (r * Math.cos(angle * 2 * Math.PI / 360.0)) + Constant.Y_CENTER; 
//		
//		setPosition(x, y);
//		
//		if (parent.getRemaining() == 0) {
//			setVisible(false);
//		}
//		System.out.println("them vit");
//	}
	
	@Override
	public void act(float delta) {
		super.act(delta);
		invalidate();
//		if (StatusEnum.IDLE.equals(run)) {
//			if (MyInput.pressKey(Keys.SPACE)) {
//				run = StatusEnum.RUNNING; 
//				init();
//				setVisible(true);
//				
//			}
//		}
//		else 
			if (StatusEnum.RUNNING.equals(run)) {
			angle += Constant.DUCK_SPEED;
		
			x = (int) (r * Math.sin(angle * 2 * Math.PI / 360.0)) + Constant.X_CENTER; 
			y = (int) (r * Math.cos(angle * 2 * Math.PI / 360.0)) + Constant.Y_CENTER; 
			
			if (angle == 360) {
				angle = 0;
			}
			
			setPosition(x, y);
			
			if (Lake.isTouch) {
				Lake.isTouch = false;
				run = StatusEnum.GOING_HOME;
				SoundManager.playSound(SoundManager.SOUND_DUCK_1);
				goHome(angle);
			}
		}
		else if (StatusEnum.GOING_HOME.equals(run)) {
			//neu vit da ve toi nha.
			
			xCollision = (int) (10 * Math.sin(angle * 2 * Math.PI / 360.0)); 
			yCollision = (int) (10 * Math.cos(angle * 2 * Math.PI / 360.0));
//			Gdx.app.log(String.valueOf(xCollision), String.valueOf(yCollision));
			
			if (getActions().size == 0) {
				SoundManager.playSound(SoundManager.SOUND_DUCK_2);
				setRotation(0);
				drawable.setRegion(GameControl.getAtlas().findRegion("play/duck", 1));
				setWidth(drawable.getMinWidth());
				setHeight(drawable.getMinHeight());
//				invalidate();
				parent.goHome();
				if (parent.getRemaining() > 0)
					run = StatusEnum.RUNNING;
				else{
						run = StatusEnum.IDLE;
						SoundManager.playSound(SoundManager.SOUND_DUCK_3);
					}
			}
			//neu vit dang boi ve nha.
		}
		
		checkCollision();
		
		point.setX(getX() + xCollision);
		point.setY(getY() + yCollision);
	}
	
	private void checkCollision(){
		Actor a = parent.getLacosteGroup().hit(getX(), getY(), false);
		if (a != null) 
			if (a instanceof Lacoste)
				if (((Lacoste) a).isHidden() == false)
					if (bOneCollision == false){
						bOneCollision = true;
						System.out.println("Cá sấu ăn vịt");
						Constant.decLives();
						PlayScreen.duckLose();
						this.remove();
		}
	}
	
	public void goHome(float angle) {
		drawable.setRegion(GameControl.getAtlas().findRegion("play/duck", 3));
		setWidth(drawable.getMinWidth());
		setHeight(drawable.getMinHeight());
		
//		invalidate();
		setRotation(360 - angle);
		
		MoveToAction move = new MoveToAction();
		move.setPosition(Constant.X_CENTER, Constant.Y_CENTER);
		move.setDuration(Constant.DURATION);
		
		addAction(move);
	}
}